<template>
  <div>
    <v-edit :title="init?.isAdd ? '用户新增' : '用户编辑'" :init="comm" :infos="info" @reset="reset" @save="save" @close="close"
      :rules="rules">
      <template #formItem>
        <div class="two-col intro-y">
          <wde-input label="用户账户" prop="userAcc" v-model="info.userAcc" />
          <wde-switch label="管理员" v-model="info.adminFlag" />
          <!--          <el-form-item label="角色" :prop="prop">-->
          <!--            <el-radio-group v-model="info.roleId">-->
          <!--              <el-radio :value="9">管理员</el-radio>-->
          <!--              <el-radio :value="1">领导</el-radio>-->
          <!--              <el-radio :value="2">申报员</el-radio>-->
          <!--            </el-radio-group>-->
          <!--          </el-form-item>-->
        </div>
        <div class="two-col intro-y">
          <wde-input label="用户名" prop="userName" v-model="info.userName" />
          <!-- <wde-switch label="企业" v-model="info.companyFlag"/> -->
          <wde-switch label="停用" v-model="info.stopFlag" />
        </div>
        <div class="two-col intro-y" v-if="init.isAdd">
          <wde-pwd label="密码" :disabled="!init.isAdd" prop="userPwd" v-model="info.userPwd" />
          <!-- <wde-switch label="停用" v-model="info.stopFlag"/> -->
        </div>
        <div class="two-col intro-y" v-if="init.isAdd">
          <wde-pwd label="确认密码" :disabled="!init.isAdd" v-model="repeatPwd" />
        </div>
        <div class="two-col intro-y">
          <wde-input label="职务" prop="post" v-model="info.post" />
          <wde-select label="性别" v-model="info.sex" :options="sexList" />
        </div>
        <div class="two-col intro-y">
          <wde-date label="出生日期" dateType="date" v-model="info.birthday" />
          <wde-input label="邮箱" prop="email" v-model="info.email" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="证件类型" v-model="info.idType" :options="idTypeList" />
          <wde-input label="证件号" prop="idCode" v-model="info.idCode" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="民族" v-model="info.nation" :options="nationList" />
          <wde-input label="籍贯" prop="native" v-model="info.native" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="学历" v-model="info.edtLevel" :options="edtList" />
        </div>
        <div class="two-col intro-y">
          <wde-input label="毕业院校" prop="school" v-model="info.school" />
          <wde-input label="专业" prop="major" v-model="info.major" />
        </div>
        <div class="one-col intro-y">
          <wde-input label="地址" prop="addr" v-model="info.addr" />
        </div>
        <!-- <div class="two-col intro-y">
          <wde-input label="联系人" prop="linker" v-model="info.companyLinker" />
          <wde-input label="联系电话" prop="linkerTel" v-model="info.companyLinkerTel" />
        </div> -->
<!-- 
        <div class="one-col intro-y">
          <wde-input label="地址" prop="addr" v-model="info.addr" />
        </div> -->
        <div class="two-col intro-y">
          <wde-input label="邮编" prop="zipCode" v-model="info.zipCode" />
        </div>
      
        <div class="one-col intro-y">
          <wde-input label="备注" prop="remark" v-model="info.remark" type="textarea" />
        </div>
        <!-- <div class="two-col intro-y">
          <wde-price label="金额(万元)" v-model="info.price" />
        </div>
        <div class="two-col intro-y">
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="项目类型" v-model="info.type" :options="options" />
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-date label="计划开始时间" dateType="date" v-model="info.date" />
        </div>
        <div class="two-col intro-y">
          <wde-switch label="是否激活" v-model="info.isActive" />
        </div> -->
      </template>
    </v-edit>
  </div>
</template>

<script setup>
import { ref, toRefs, watch, getCurrentInstance, onMounted } from "vue";
import { useEditLogic } from "@/views/base/useEditLogic";
import * as $api from "@/api/api.js";
import { ElMessage } from 'element-plus'
import { ReferDict } from "@/api/api.js";

const { proxy } = getCurrentInstance();
const emit = defineEmits(["close"]);

const props = defineProps({
  args: {
    type: Object,
    default: () => null,
  },
  init: {
    type: Object,
    default: () => null,
  },
});
// 使用 toRefs 解构 props
const { init } = toRefs(props);
const info = ref({
  userKey: "",
  userAcc: "",
  userName: "",
  userPwd: "",
  sex: 0,
  birthday: "2024-12-03T11:55:48.959Z",
  post: "",
  iconUrl: "",
  idType: 0,
  idCode: "",
  addr: "",
  zipCode: "",
  email: "",
  edtLevel: 0,
  school: 0,
  major: 0,
  nation: 0,
  native: "",
  adminFlag: true,
  stopFlag: true,
  remark: "",
  companyFlag: true,
  companyName: "",
  companyRegion: "",
  companyCity: "",
  companyAddr: "",
  companyEmail: "",
  companyLinker: "",
  companyLinkerTel: "",
  companyLeader: "",
  companyTel: ""
});
const userKey = ref(localStorage.getItem("userKey"));
const repeatPwd = ref("");
// const info = ref({
//   tchKey: "",
//   tchNum: "",
//   tchName: "",
//   sex: true,
//   tel: "",
//   email: "",
//   title: "",
//   birthday: "",
//   nation: "",
//   idType: "",
//   idCode: "",
//   stopFlag: true,
//   remark: "",
// });
const idTypeList = ref([]);
const nationList = ref([]);
const edtList = ref([]);
const sexList = ref([]);
const options = ref([
  {
    value: "Option1",
    label: "Option1",
  },
  {
    value: "Option2",
    label: "Option2",
  },
  {
    value: "Option3",
    label: "Option3",
  },
  {
    value: "Option4",
    label: "Option4",
  },
  {
    value: "Option5",
    label: "Option5",
  },
]);
const rules = ref({
  userAcc: [
    { required: true, message: "请输入用户账户", trigger: "blur" },
  ],
  userName: [
    { required: true, message: "请输入用户名", trigger: "blur" },
  ],
  userPwd: [
    { required: true, message: "请输入密码", trigger: "blur" },
  ],
  repeatPwd: [
    { required: true, message: "请输入确认密码", trigger: "blur" },
  ]
});
onMounted(
  () => {
    getIdTypeList();
    getNationList();
    getEdtList();
    getSexList();
  }
)
const getIdTypeList = () => {
  $api.ReferDict({
    plantKey: "",
    userKey: userKey.value,
    key: "10"
  }).then((res) => {
    idTypeList.value = res.subs.map(item => {
      return {
        value: item.dictID,
        label: item.dictName,
      }
    });
  });
};
const getNationList = () => {
  $api.ReferDict({
    plantKey: "",
    userKey: userKey.value,
    key: "30"
  }).then((res) => {
    nationList.value = res.subs.map(item => {
      return {
        value: item.dictID,
        label: item.dictName,
      }
    })
  })
}
const getEdtList = () => {
  $api.ReferDict({
    plantKey: "",
    userKey: userKey.value,
    key: "20"
  }).then((res) => {
    edtList.value = res.subs.map(item => {
      return {
        value: item.dictID,
        label: item.dictName,
      }
    })
  })
};
const getSexList = () => {
  $api.ReferDict({
    plantKey: "",
    userKey: userKey.value,
    key: "40"
  }).then((res) => {
    sexList.value = res.subs.map(item => {
      return {
        value: item.dictID,
        label: item.dictName,
      }
    })
  })
}
const queryOne = () => {
  $api.UserQueryOne({
    plantKey: "",
    userKey: props.init.userKey
  }).then((res) => {
    // proxy.$propsCopy(info.value, res);
    info.value.userPwd = res.userPwd
  });
}
const buildAdd = () => {
  console.log(info.value);
  if (info.value.userPwd != repeatPwd.value) {
    ElMessage.error('两次密码不一致')
    return;
  }
  return $api.UserInsert(info.value);
  // 构建新增逻辑
};

const buildUpdate = () => {
  return $api.UserUpdate(info.value);
  // return $api.BATeacherUpdate(info.value);
};
const resetAdd = () => {
  proxy.$propsEmpty(info.value);
  // proxy.$propsCopy(info.value, props.init);
};
const resetUpdate = () => {
  proxy.$propsCopy(info.value, props.init);
};
const clearInfo = () => {
  proxy.$propsEmpty(info.value);
};

const close = () => {
  emit("close");
};
const { loading, comm, isView, reset, save, VEdit } = useEditLogic(
  init,
  info,
  resetAdd,
  resetUpdate,
  clearInfo,
  buildAdd,
  buildUpdate,
  close
);
watch(
  () => comm,
  (newVal) => {
    console.log(newVal);
  }
);
watch(
  () => props.init,
  (newVal) => {
    if (newVal) {
      repeatPwd.value = newVal.userPwd;
      queryOne();
    }
  }
);
</script>

<style></style>